package com.cays.myresearch.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.cays.myresearch.MainActivity;
import com.cays.myresearch.R;
import com.cays.myresearch.Util.HttpUtil;
import com.cays.myresearch.activity.QuestionActivity;
import com.cays.myresearch.activity.QuestionInfoActivity;
import com.cays.myresearch.adapter.DanielItemAdapter;
import com.cays.myresearch.adapter.HallSettingAdapter;
import com.cays.myresearch.adapter.QuestionAdapter;
import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.HallSetting;
import com.cays.myresearch.entity.Question;
import com.cays.myresearch.entity.Teacher;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by lenovo on 2018/6/23.
 */

public class SeminarHallFragment extends BaseFragment implements View.OnClickListener,HallSettingAdapter.ItemClickListener
        ,QuestionAdapter.QuestionItemClickListener{
    private View view;
    private RecyclerView oftenQuestionRV,questionRV,hallSettingRv;
    private HallSettingAdapter hallSettingAdapter;
    private QuestionAdapter questionAdapter,oftenQuestionAdapter;
    private List<HallSetting> hallSettings=new ArrayList<>();
    private List<Question> questionList=new ArrayList<Question> ();
    private List<Question> oftenQuestionList=new ArrayList<Question> ();
    private PtrFrameLayout hallRefreshPtr;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(view==null) {
            view = inflater.inflate(R.layout.seminar_hall_layout, container, false);
            hallSettingRv = (RecyclerView) view.findViewById(R.id.hall_lists);
            oftenQuestionRV = (RecyclerView) view.findViewById(R.id.hall_often_question_rv);
            questionRV = (RecyclerView) view.findViewById(R.id.hall_question_rv);
            hallRefreshPtr = (PtrFrameLayout) view.findViewById(R.id.hall_refresh);
            //questionPtr=(PtrFrameLayout)view.findViewById(R.id);
            intiHallSetting();
            initQuestion();
            initFresh();
        }
        return view;
    }
    private void intiHallSetting(){
        hallSettings.clear();
        HallSetting hallSetting1=new HallSetting("常见问题",R.drawable.c3);
        hallSettings.add(hallSetting1);
        HallSetting hallSetting3=new HallSetting("科研论坛",R.drawable.c3);
        hallSettings.add(hallSetting3);
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),2);
        hallSettingRv.setLayoutManager(layoutManager);
        hallSettingAdapter=new HallSettingAdapter(hallSettings);
        hallSettingRv.setAdapter(hallSettingAdapter);
        hallSettingAdapter.setOnItemClickListener(this);
    }
    private void initQuestion(){
        questionList.clear ();oftenQuestionList.clear();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager (getActivity ());
        linearLayoutManager.setOrientation (LinearLayout.VERTICAL);
        questionAdapter=new QuestionAdapter (questionList);
        questionRV.setLayoutManager (linearLayoutManager);
        questionRV.setAdapter (questionAdapter);
        questionAdapter.setItemClickListener (this);

        LinearLayoutManager linearLayoutManager2=new LinearLayoutManager (getActivity ());
        linearLayoutManager2.setOrientation (LinearLayout.VERTICAL);
        oftenQuestionAdapter=new QuestionAdapter (oftenQuestionList);
        oftenQuestionRV.setLayoutManager (linearLayoutManager2);
        oftenQuestionRV.setAdapter (oftenQuestionAdapter);
        oftenQuestionAdapter.setItemClickListener (this);
    }
    public void initFresh(){
        //这里是一个自定义的头部刷新布局,自带的也有一个布局new PtrDefaultHandler();
        PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(getActivity());
        hallRefreshPtr.addPtrUIHandler(header);
        //不仅仅是添加头布局,还需要设置到控件中 注:特别重要,不然没显示
        hallRefreshPtr.setHeaderView(header);
        hallRefreshPtr.postDelayed(new Runnable() {
            @Override
            public void run() {
                //执行调用下拉刷新监听
                hallRefreshPtr.autoRefresh(true);
            }
        },200);
        //设置下拉刷新监听
        hallRefreshPtr.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                // 默认实现，根据实际情况做改动
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                refreshQuestion();
            }
        });
    }
    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==1){
                oftenQuestionAdapter.notifyDataSetChanged();
                questionAdapter.notifyDataSetChanged();
                hallRefreshPtr.refreshComplete();
            }
            return true;
        }
    });
    //下拉刷新事件
    private void refreshQuestion(){
        final String url=Constant.URL+"question/often_question";
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendHttpRequest(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        oftenQuestionList.clear();
                        questionList.clear();
                        Message message=new Message();
                        message.what=1;
                        handler.sendMessage(message);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String s=response.body().string(),info="",oftenStr="",questionStr;
                        oftenQuestionList.clear();
                        questionList.clear();
                        if(s.contains("~")){
                            oftenStr=s.split("~")[1];
                            questionStr=s.split("~")[2];
                            List<Question> oftenList=Constant.gson.fromJson(oftenStr,
                                    new TypeToken<List<Question>>(){}.getType());
                            List<Question> qList=Constant.gson.fromJson(questionStr,
                                    new TypeToken<List<Question>>(){}.getType());
                            for (int i=0;i<5&&i<oftenList.size()&&i<qList.size();i++){
                                oftenQuestionList.add(oftenList.get(i));
                                questionList.add(qList.get(i));
                            }
                        }
                        Message message=new Message();
                        message.what=1;
                        handler.sendMessage(message);
                    }
                });
            }
        }).start();
    }
    @Override
    public void onResume() {
        super.onResume();
        MainActivity.currTag= Constant.fragment_seminar_hall;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onItemClick(int position) {
        switch (position){
            case 0:
                Intent intent1=new Intent(getActivity(), QuestionActivity.class);
                intent1.putExtra("often_question",true);
                startActivity(intent1);break;
            case 1:
                Intent intent=new Intent(getActivity(), QuestionActivity.class);
                startActivity(intent);break;
            default:break;
        }
    }
    @Override
    public void questionItemClick(List<Question> questions,int position) {
        Intent intent=new Intent(getActivity(), QuestionInfoActivity.class);
        String q=Constant.gson.toJson(questions.get(position));
        intent.putExtra("intent_question",q);
        startActivity(intent);
    }
}
